package com.golddaymicromall.activity;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.golddaymicromall.jpush.ExampleUtil;
import com.golddaymicromall.util.SPUtils;
import cn.jpush.android.api.InstrumentedActivity;
import cn.jpush.android.api.JPushInterface;

public class WelcomeActivity extends InstrumentedActivity {
    protected static final String tag = "WelcomeActivity";
    String flag;
    String id;
    public static boolean isForeground = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
        registerMessageReceiver();
        init();
        flag = SPUtils.get(getApplicationContext(), "flag", "").toString();
        id = SPUtils.get(getApplicationContext(), "Id", "").toString();
        final SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        final boolean b = sharedPreferences.getBoolean("isFirst", true);
        //1，第几次进入当前应用（1，导航页  2....mainActivity）
        //handler延时发送消息
        new Handler() {
            public void handleMessage(android.os.Message msg) {
                //3秒过后回去执行的逻辑
                if (b) {
                    //第一次进入，进入导航页,将当前isFirst做修改
                    sharedPreferences.edit().putBoolean("isFirst", false).commit();
                    Intent intent = new Intent(WelcomeActivity.this, GuideActivity.class);
                    startActivity(intent);
                } else {
                    //第二次或以后,进入主页面
                    if ("".equals(flag) || ("".equals(id))) {
                        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else if ("".equals(id)) {
                        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else if (!"".equals(id)) {
                        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                }
                finish();
            }

            ;
        }.sendEmptyMessageDelayed(0, 2000);
    }

    // 初始化 JPush。如果已经初始化，但没有登录成功，则执行重新登录。
    private void init() {
        JPushInterface.init(getApplicationContext());
    }


    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }


    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }


    //for receive customer msg from jpush server
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.golddaymicromall.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                String messge = intent.getStringExtra(KEY_MESSAGE);
                String extras = intent.getStringExtra(KEY_EXTRAS);
                StringBuilder showMsg = new StringBuilder();
                showMsg.append(KEY_MESSAGE + " : " + messge + "\n");

                if (!ExampleUtil.isEmpty(extras)) {
                    showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                }
                setCostomMsg(showMsg.toString());
            }
        }
    }

    private void setCostomMsg(String msg) {

    }
}
